require("stategraphs/commonstates")

local actionhandlers =
{
    -- ActionHandler(ACTIONS.HELP_FARMING,function(inst, action)
    --     return 'help_farming'
    -- end),

}

local events =
{
    EventHandler("locomote", function(inst)
        local is_moving = inst.sg:HasStateTag("moving")
        local is_idling = inst.sg:HasStateTag("idle")

        local should_move = inst.components.locomotor:WantsToMoveForward()

        if is_moving and not should_move then
            inst.sg:GoToState("idle")
        elseif is_idling and should_move then
            inst.sg:GoToState("walk")
        end
    end),
    EventHandler("death", function(inst) inst.sg:GoToState("death") end),
}

local states = {
    State {
        name = "walk",
        tags = { "moving", "canrotate" },

        onenter = function(inst)
            inst.components.locomotor:WalkForward()
            inst.AnimState:PlayAnimation("walk", true)
            inst.SoundEmitter:PlaySound("rifts2/parasitic_shadeling/dreadmite_walk", "walk")
            inst.sg:SetTimeout(inst.AnimState:GetCurrentAnimationLength())
        end,

        ontimeout = function(inst)
            inst.sg:GoToState("walk")
        end,

        onexit = function(inst)
            inst.SoundEmitter:KillSound("walk")
        end,
    },
    State {
        name = 'death',
        tags = { "busy" },
        onenter = function(inst)
            inst.AnimState:PlayAnimation("death")
            inst.Physics:Stop()
        end,
        events =
        {
            EventHandler("animover", function(inst)
                inst:Remove()
            end),
        },
    },
    State {
        name = 'help_farming',
        tags = { "busy" },
        onenter = function(inst)
            inst.AnimState:PlayAnimation("bounce")
            inst.Physics:Stop()
        end,
        timeline = {
            TimeEvent(13*FRAMES, function(inst) inst:PerformBufferedAction() end),
        },
        events =
        {
            EventHandler("animover", function(inst)
                inst.sg:GoToState("idle")
            end),
        },
    },
    State {
        name = 'sleep',
        tags = { "busy", 'gotosleep' },
        ---comment
        ---@param inst ent
        onenter = function(inst)
            inst.Physics:Stop()
            inst.components.locomotor:Stop()
            inst.AnimState:PlayAnimation("spawn")
            inst.AnimState:SetTime(20*FRAMES)
            inst.AnimState:SetDeltaTimeMultiplier(-1)

            -- inst.AnimState:PushAnimation('idle_ground',true)
        end,
        timeline = {
            TimeEvent(20*FRAMES, function(inst)
                inst.sg:GoToState("sleeping")
                inst.AnimState:SetDeltaTimeMultiplier(1)
            end),
        },
        events =
        {
            EventHandler("animover", function(inst)
                inst.sg:GoToState("sleeping")
                inst.AnimState:SetDeltaTimeMultiplier(1)
            end),
        },
    },
    State {
        name = 'sleeping',
        tags = { "busy", 'sleeping' },
        ---comment
        ---@param inst ent
        onenter = function(inst)
            inst.Physics:Stop()
            inst.AnimState:PlayAnimation("idle_ground",true)
        end,
    },
}

-- CommonStates.AddSimpleState(states, "bounce", "bounce", {"busy"}, "idle_ground")
CommonStates.AddSimpleState(states, "idle_ground", "idle_ground", { "idle" }, "idle_ground")
CommonStates.AddSimpleState(states, "spawn", "spawn", {"busy"}, 'idle')
CommonStates.AddSimpleState(states, "idle", "idle_2", { "idle", "canrotate" })
CommonStates.AddSimpleState(states, "wakeup", "spawn", {"busy"}, 'idle')

return StateGraph("webbert_skitter", states, events, "spawn", actionhandlers)